package algo3c3g3.view;

import algo3c3g3.model.action.*;
import javax.swing.*;
import java.awt.event.*;
import algo3c3g3.model.*;
import algo3c3g3.controller.*;
import java.util.*;

/**
 * Contenedor de botones para seleccionar acciones.
 * Maneja los eventos de esos botones. Al presionar alguno, se le inidca
 * al controlador la accion seleccionada.
 */
public class ActionPanel extends JPanel implements Observer
{
    private ActionController controller;
    private ActionsAvailable actions;
    
    private JButton
        basher, digger, blocker, builder, floater, teleporter;
    
    
    /**
     * @param level Modelo de datos asociado.
     * @param controller Controlador del modelo asociado.
     */
    public ActionPanel(Level level, Controller controller)
    {
        this.controller = controller.getActionController();
        this.actions = level.getActionsAvailable();
        
        add(basher = new ActionButton("Laser (" + actions.getQuantity(Basher.class) + ")", Basher.class));
        add(digger = new ActionButton("Taladro (" + actions.getQuantity(Digger.class) + ")", Digger.class));
        add(blocker = new ActionButton("Congelar (" + actions.getQuantity(Blocker.class) + ")", Blocker.class));
        add(builder = new ActionButton("Tunel (" + actions.getQuantity(Builder.class) + ")", Builder.class));
        add(floater = new ActionButton("Platillo (" + actions.getQuantity(Floater.class) + ")", Floater.class));
        add(teleporter = new ActionButton("Teleport (" + actions.getQuantity(Teleporter.class) + ")", Teleporter.class));
        
        basher.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                ActionPanel.this.controller.selectAction(Basher.class);
            }
        });
        
        digger.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                ActionPanel.this.controller.selectAction(Digger.class);
            }
        });
        
        blocker.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                ActionPanel.this.controller.selectAction(Blocker.class);
            }
        });
        
        builder.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                ActionPanel.this.controller.selectAction(Builder.class);
            }
        });
        
        floater.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                ActionPanel.this.controller.selectAction(Floater.class);
            }
        });
        
        teleporter.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                ActionPanel.this.controller.selectAction(Teleporter.class);
            }
        });
        
        actions.addObserver(this);
    }
    
    /**
     * Actualiza las cantidades disponibles de las acciones.
     */
    public void update(Observable o, Object arg)
    {
        basher.setText("Laser (" + actions.getQuantity(Basher.class) + ")");
        digger.setText("Taladro (" + actions.getQuantity(Digger.class) + ")");
        blocker.setText("Congelar (" + actions.getQuantity(Blocker.class) + ")");
        builder.setText("Tunel (" + actions.getQuantity(Builder.class) + ")");
        floater.setText("Platillo (" + actions.getQuantity(Floater.class) + ")");
        teleporter.setText("Teleport (" + actions.getQuantity(Teleporter.class) + ")");
    }
}
